<template>
	<div>
		<!-- 猜你喜欢 -->
		<div class="caption">
			<div class="text">
				猜你喜欢
			</div>
		</div>
		<div class="waterfall">
			<div v-for="item in recommendedItems" :key="item.id" class="waterfall-item">
				<div class="guess-item" @tap="gotoPro(item.id)">
					<image class="image" :src="loadUrl + item.image" alt="商品图片" />
					<div class="info">
						<div class="name">
							{{ item.title }}<br>
							工厂直营 质量保证
						</div>
						<div class="price">
							<div>
								<span class="small">¥</span>
								<span class="big">{{ item.price /100 }}</span>
							</div>
							<div>
								<button class="add-button" @click.stop="addToCart(item)">+</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div v-if="loading" class="loading-text">
			正在加载...
		</div>
		<div v-if="noMoreData" class="bottom-line">
			<div class="line"></div>
			<div class="text">我也是有底线的</div>
			<div class="line"></div>
		</div>
	</div>
</template>

<script>
	import axios from 'axios';

	export default {
		props: ['isLoading'],
		data() {
			return {
				recommendedItems: [],
				loading: false,
				loadUrl: this.$loadUrl,
				page: 1,
				noMoreData: false
			};
		},
		watch: {
			isLoading(newValue, oldValue) {
				this.fetchRecommendedItems();
			}
		},
		methods: {
			gotoPro(id) {
				uni.setStorageSync('currentItemId', id)
				uni.navigateTo({
					url: '/pages/goods/goods'
				})
			},
			fetchRecommendedItems() {
				if (this.loading || this.noMoreData) return;

				this.loading = true;
				this.$service({
					url: `/items/recommend/${this.page}`,
					method: 'GET',
				}).then(response => {
					if (response.code === 0) {
						if (response.data.length === 0) {
							this.noMoreData = true;
						} else {
							this.recommendedItems = [...this.recommendedItems, ...response.data];
							this.page++;
						}
					}
					this.$emit('loadDone')
					this.loading = false;
				})
			},
			addToCart(item) {
				this.$service({
					url: '/carts',
					method: 'POST',
					data: {
						itemId: item.id
					}
				}).then(response => {
					if (response.code === 0) {
						console.log('添加到购物车成功', response.data);
						uni.showToast({
							title: '添加购物车成功',
							duration: 2000
						});
					}
				}).catch(error => {
					console.error('添加到购物车失败', error);
				});
			}
		},
		mounted() {
			this.fetchRecommendedItems();
		}
	}
</script>

<style scoped>
	.caption {
		display: flex;
		justify-content: center;
		line-height: 1;
		padding: 16px 0 20px;
		font-size: 24px;
		color: #262626;
	}

	.caption .text {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 14px 0 15px;
		font-size: 15px;
	}

	.caption .text::before,
	.caption .text::after {
		content: '';
		width: 16px;
		height: 16px;
		background-image: url('/static/images/bubble.png');
		background-size: contain;
		margin: 0 8px;
	}

	.waterfall {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 0 10px;
	}

	.waterfall-item {
		width: calc(50% - 5px);
		margin-bottom: 10px;
	}

	.guess-item {
		background-color: #fff;
		border-radius: 8px;
	}

	.image {
		width: 47vw;
		height: 47vw;
		background-size: cover;
		background-position: center;
	}

	.info {
		padding: 8px;
	}

	.name {
		font-size: 14px;
		color: #262626;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		margin-bottom: 8px;
	}

	.price {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.small {
		font-size: 12px;
		color: #cf4444;
	}

	.big {
		font-size: 18px;
		color: #cf4444;
		font-weight: bold;
	}

	.add-button {
		width: 24px;
		height: 24px;
		background-color: #cf4444;
		border: none;
		border-radius: 50%;
		font-size: 18px;
		color: #ffffff;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.loading-text {
		text-align: center;
		font-size: 14px;
		color: #666;
		padding: 16px 0;
	}

	.bottom-line {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 16px 0;
	}

	.bottom-line .line {
		flex: 1;
		height: 1px;
		background-color: #e0e0e0;
	}

	.bottom-line .text {
		padding: 0 16px;
		font-size: 14px;
		color: #999;
	}
</style>